package com.bw.day03.catexq

import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.base.net.mvvm.BaseActivity
import com.bw.day03.R
import com.bw.day03.adapter.GoodsAdapter
import com.bw.day03.databinding.ActivityCateXqBinding

@Route(path = "/app/CateXqActivity")
class CateXqActivity : BaseActivity<ActivityCateXqBinding, CateXqViewModel>() {
    @Autowired
    @JvmField
    var cname=""
    @Autowired
    @JvmField
    var cid=0
    var keyword=""
    var limit=10
    var news=false
    var page=1
    var priceOrder=""
    var salesOrder=""
    lateinit var goodsAdapter: GoodsAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.tvType.text=cname
        viewModel.cateXq(cid,keyword,limit,news,page,priceOrder,salesOrder)

        goodsAdapter= GoodsAdapter()
        binding.rvGoods.adapter=goodsAdapter
        binding.rvGoods.layoutManager=GridLayoutManager(this,2)

        binding.tvNew.setOnClickListener {
            if (news){
                binding.tvNew.setTextColor(Color.BLACK)
                news=false
            }else{
                binding.tvNew.setTextColor(Color.BLUE)
                news=true
            }
            viewModel.cateXq(cid,keyword,limit,news,page,priceOrder,salesOrder)

        }

        binding.tvSale.setOnClickListener {
            if (salesOrder==""){
                salesOrder="asc"
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img2,0)
            }else if (salesOrder=="asc"){
                salesOrder="desc"
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img1,0)
            }else{
                salesOrder=""
                binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img3,0)
            }
            binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img3,0)
            priceOrder=""
            viewModel.cateXq(cid,keyword,limit,news,page,priceOrder,salesOrder)
        }
        binding.tvPrice.setOnClickListener {
            if (priceOrder==""){
                priceOrder="asc"
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img2,0)
            }else if (priceOrder=="asc"){
                priceOrder="desc"
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img1,0)
            }else{
                priceOrder=""
                binding.tvPrice.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img3,0)
            }

            binding.tvSale.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.img3,0)
            salesOrder=""
            viewModel.cateXq(cid,keyword,limit,news,page,priceOrder,salesOrder)
        }
        binding.etSearch.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                keyword=s.toString()
                viewModel.cateXq(cid,keyword,limit,news,page,priceOrder,salesOrder)
            }

            override fun afterTextChanged(s: Editable?) {

            }
        })
        goodsAdapter.setOnItemClickListener{adapter,view,position->
            ARouter.getInstance().build("/app/XqActivity").withInt("goodsId",goodsAdapter.data[position].id).navigation()
        }


    }
    override fun initData() {
        viewModel.cateXqSuccess.observe (this){
            if (it.code==200){
                goodsAdapter.data.clear()
                 goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CateXqViewModel> =  CateXqViewModel::class.java
    override fun getLayoutId(): Int = R.layout.activity_cate_xq

}